package com.smalls.newvideotwo.auth;

import android.os.AsyncTask;

import com.smalls.newvideotwo.BuildConfig;
import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.AppUtils;
import com.smalls.newvideotwo.utils.Constant;
import com.uking.helper.VKNetBossUtils;
import com.uking.helper.VKPortalProxy;
import com.uking.util.VKConstant;

public class AAARegisterTask extends AsyncTask<String, Void, AAARegisterResult> {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "AAARegisterTask":"AAARegisterTask";
    private OnResultListener onResultListener;
    private String m3A = null;

    public interface OnResultListener {
        void onResult(AAARegisterResult uid);
    }

    public AAARegisterTask setOnResultListener(OnResultListener onResultListener) {
        this.onResultListener = onResultListener;
        return this;
    }

    public void set3A(String aaa) {
        m3A = aaa;
    }

    protected AAARegisterResult doInBackground(String... params) {
        if(m3A==null) {
            return null;
        }
        LogUtil.d(TAG, "aaa domain1="+ m3A);
        LogUtil.d(TAG, "packageName=" + AppUtils.getPackageName(NewDTVApp.getApp()));
        String code = "";
        if(params!=null&&params.length>0) {
            code = params[0];
        } else {
            code = VKNetBossUtils.decrypt(NewDTVApp.sharedPre.getString("uid",""),
                    Constant.KEY);
        }
        LogUtil.d(TAG,"code="+code);
        NewDTVApp.getApp().setToken(null);
        String result = new VKPortalProxy(NewDTVApp.getApp().sharedPre).registerUser(m3A,
                code,
                AppUtils.getPackageName(NewDTVApp.getApp()));
        LogUtil.d(TAG,"result="+result);
        if(result!=null&&result.length()>0) {
            AAARegisterResult aaaActivateResult = new AAARegisterResult();
            String encryptAuthString = "";
            String[] paramLists = result.split("&");
            for(int i=0;i<paramLists.length;i++) {
                if(isCancelled()){
                    break;
                }
                if(paramLists[i].regionMatches(false,0,"CODE",0,4)) {
                    aaaActivateResult.CODE = paramLists[i].substring("CODE=".length());
                    LogUtil.d(TAG,"CODE="+aaaActivateResult.CODE);
                } else if(paramLists[i].regionMatches(false,0,"INFO",0,4)) {
                    aaaActivateResult.INFO = paramLists[i].substring("INFO=".length());
                    LogUtil.d(TAG,"INFO="+aaaActivateResult.INFO);
                } else if(paramLists[i].regionMatches(false,0,"USERID",0,5)) {
                    aaaActivateResult.USERID = paramLists[i].substring("USERID=".length());
                    LogUtil.d(TAG,"USERID="+aaaActivateResult.USERID);
                } else if(paramLists[i].regionMatches(false,0,"AUTHTM",0,6)) {
                    aaaActivateResult.AUTHTM = paramLists[i].substring("AUTHTM=".length());
                    LogUtil.d(TAG,"AUTHTM="+aaaActivateResult.AUTHTM);
                    encryptAuthString = VKNetBossUtils.encrypt(aaaActivateResult.AUTHTM, Constant.KEY);
                    NewDTVApp.getApp().sharedPre.edit().putString(Constant.AAA_REGISTER_AUTH_TIME, encryptAuthString).commit();
                }else if(paramLists[i].regionMatches(false,0,"TOKEN",0,5)) {
                    aaaActivateResult.TOKEN = paramLists[i].substring("TOKEN=".length());
                    LogUtil.d(TAG,"TOKEN="+aaaActivateResult.TOKEN);
                    NewDTVApp.getApp().setToken(aaaActivateResult.TOKEN);
                    encryptAuthString = VKNetBossUtils.encrypt(aaaActivateResult.TOKEN, Constant.KEY);
                    NewDTVApp.getApp().sharedPre.edit().putString(VKConstant.AUTHSTRING_REGISTER,encryptAuthString).commit();
                }else if(paramLists[i].regionMatches(false,0,"EPGSVR",0,6)) {
                    aaaActivateResult.EPGSVR = paramLists[i].substring("EPGSVR=".length());
                    LogUtil.d(TAG,"EPGSVR="+aaaActivateResult.EPGSVR);
                    NewDTVApp.getApp().parseEpgserver(aaaActivateResult.EPGSVR);
                    encryptAuthString = VKNetBossUtils.encrypt(aaaActivateResult.EPGSVR, Constant.KEY);
                    NewDTVApp.getApp().editor.putString(Constant.EPGSERVER_REGISTER,encryptAuthString).commit();
                    NewDTVApp.getApp().setmEpgServer();
                }else if(paramLists[i].regionMatches(false,0,"VODGSLBSVR",0,10)) {
                    aaaActivateResult.VODGSLBSVR = paramLists[i].substring("VODGSLBSVR=".length());
                    LogUtil.d(TAG,"VODGSLBSVR="+aaaActivateResult.VODGSLBSVR);
                }else if(paramLists[i].regionMatches(false,0,"LIVEGSLBSVR",0,11)) {
                    aaaActivateResult.LIVEGSLBSVR = paramLists[i].substring("LIVEGSLBSVR=".length());
                    LogUtil.d(TAG,"LIVEGSLBSVR="+aaaActivateResult.LIVEGSLBSVR);
                }else if(paramLists[i].regionMatches(false,0,"AREAID",0,6)) {
                    aaaActivateResult.AREAID = paramLists[i].substring("AREAID=".length());
                    LogUtil.d(TAG,"AREAID="+aaaActivateResult.AREAID);
                }
            }
            return aaaActivateResult;
        }
        return null;
    }

    protected void onPostExecute(AAARegisterResult aBoolean) {
        super.onPostExecute(aBoolean);
        if(isCancelled()){
            return;
        }
        if (this.onResultListener != null) {
            this.onResultListener.onResult(aBoolean);
        }
    }
}
